import 'package:all_in_onedemo/FlexLayoutRoute.dart';
import 'package:all_in_onedemo/FlowLayoutRoute.dart';
import 'package:all_in_onedemo/WrapLayoutRoute.dart';
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

void main() => runApp(AllInOne());

class AllInOne extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      // showPerformanceOverlay: true,
      routes: {
        'firstRoute': (context) => Route1(),
        'flexLayoutRoute': (context) => FlexLayoutRoute(),
        'WrapLayoutRoute': (context) => WrapLayoutRoute(),
        'FlowLayoutRoute': (context) => FlowLayoutRoute(),
        'StackRoute': (BuildContext context) => Route2(),
        'PaddingRoute': (buildContext) => PaddingTestRoute(),
        'MatrixRoute': (context) => MatrixRoute(),
        'ScrollRoute': (context) => ScrollRoute(),
        'ListViewBuilder': (context) => LVBuilderRoute(),
        'InfiniteLoadRoute': (c) => InfiniteLoadRoute(),
        'FixedItemRoute': (c) => FixedItemRoute(),
        'GridViewRoute': (c) => GridViewRoute(),
        'GetIconRoute': (c) => GetIconGDRoute(),
        'MyCustomScrollViewRoute': (c) => MyCustomGDRoute(),
        'MyScrollControllerRoute': (c) => MyScrollControllerRoute(),
        'RouteInVim': (c) => MyVimRoute(),
        'ScrollPerRoute': (c) => ScrollNotificationRoute(),
        'WillPopRoute': (c) => WillPopRoute(),
        'InheritedRoute': (c) => InheritedTestRoute(),
        'ColorChange': (c) => ChangeColorRoute(),
        'ListenerRoute': (c) => MyListenerRoute(),
        'DragRoute': (c) => MyDragRoute(),
        'MyZoomRoute': (c) => MyZoomGesture(),
      },
      home: MyHome(),
    );
  }
}

class MyHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyHomeState();
  }
}

class _MyHomeState extends State<MyHome> {
  int _selectedIndex = 1;
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  void _onAdd() {}

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        drawer: MyDrawer(),
        bottomNavigationBar: BottomNavigationBar(
          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text('home'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.business),
              title: Text('business'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.school),
              title: Text('school'),
            ),
          ],
          currentIndex: _selectedIndex,
          fixedColor: Colors.lightBlue,
          onTap: _onItemTapped,
        ),
        // endDrawer: MyEndDrawer(),
        appBar: PreferredSize(
          child: AppBar(
            elevation: 10.0,
            actions: <Widget>[
              IconButton(
                icon: Icon(Icons.share),
                onPressed: () {},
              )
            ],
            title: Text('home'),
            flexibleSpace: SizedBox(
              width: 80.0,
              height: 100.0,
              child: Padding(
                padding: const EdgeInsets.only(top: 50.0),
                child: Image.asset('lib/devpics/a.jpg'),
              ),
            ),
          ),
          preferredSize: Size.fromHeight(1920 * 0.08),
        ),
        body: Column(
          children: <Widget>[
            Wrap(
              direction: Axis.horizontal,
              children: <Widget>[
                RaisedButton(
                  child: Text('firstRoute'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'firstRoute');
                  },
                ),
                RaisedButton(
                  child: Text('flexLayout'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'flexLayoutRoute');
                  },
                ),
                RaisedButton(
                  child: Container(
                    height: 50.0,
                    width: 150,
                    child: Text('WarpLayout'),
                  ),
                  onPressed: () {
                    Navigator.pushNamed(context, 'WrapLayoutRoute');
                  },
                ),
                RaisedButton(
                  child: Text('FlowLayout'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'FlowLayoutRoute');
                  },
                ),
                RaisedButton(
                  child: Text('stack'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'StackRoute');
                  },
                ),
                RaisedButton(
                  color: Colors.lightBlue,
                  child: Text('padding'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'PaddingRoute');
                  },
                ),
                RaisedButton(
                  child: Text('matrix'),
                  color: Colors.deepOrange,
                  onPressed: () {
                    Navigator.pushNamed(context, 'MatrixRoute');
                  },
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, 'ScrollRoute');
                  },
                  child: Text('scrollable'),
                  color: Colors.red,
                ),
                RaisedButton(
                  onPressed: () {
                    Navigator.pushNamed(context, 'ListViewBuilder');
                  },
                  child: Text('ListViewBuilder'),
                  color: Colors.teal,
                ),
                RaisedButton(
                  color: Colors.lightGreen,
                  child: Text('infinite'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'InfiniteLoadRoute');
                  },
                ),
                FlatButton(
                  child: Text('Fixed'),
                  color: Colors.red[700],
                  onPressed: () {
                    Navigator.pushNamed(context, 'FixedItemRoute');
                  },
                ),
                FlatButton(
                  child: Text('gridView'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'GridViewRoute');
                  },
                ),
                FlatButton(
                  color: Colors.yellow[500],
                  child: Text('get Icon'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'GetIconRoute');
                  },
                ),
                RaisedButton(
                  color: Colors.teal[500],
                  child: Text('Custom'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'MyCustomScrollViewRoute');
                  },
                ),
                RaisedButton(
                  color: Colors.green[300],
                  onPressed: () {
                    Navigator.pushNamed(context, 'MyScrollControllerRoute');
                  },
                  child: Text('ScrollController'),
                ),
                FlatButton(
                    color: Colors.yellow[400],
                    child: Text('vim-flutter'),
                    onPressed: () {
                      Navigator.pushNamed(context, 'RouteInVim');
                    }),
                FlatButton(
                    color: Colors.green[400],
                    child: Text('scrollPercentage'),
                    onPressed: () {
                      Navigator.pushNamed(context, 'ScrollPerRoute');
                    }),
                FlatButton(
                  child: Text('WillPopScope'),
                  color: Colors.brown[200],
                  onPressed: () {
                    Navigator.pushNamed(context, 'WillPopRoute');
                  },
                ),
                FlatButton(
                  onPressed: () {
                    Navigator.pushNamed(context, 'InheritedRoute');
                  },
                  child: Text('ShareData'),
                  color: Colors.green[800],
                ),
                FlatButton(
                  child: Text('ColorChange'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'ColorChange');
                  },
                ),
                RaisedButton(
                  child: Text('listener'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'ListenerRoute');
                  },
                ),
                RaisedButton(
                  child: Text('drag'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'DragRoute');
                  },
                ),
                RaisedButton(
                  child: Text('zoom'),
                  onPressed: () {
                    Navigator.pushNamed(context, 'MyZoomRoute');
                  },
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}

class MyDrawer extends StatelessWidget {
  List<Widget> makeText() {
    var texts = List<Widget>();

    for (var i = 0; i < 50; i++) {
      texts.add(Text('Test:$i'));
    }
    return texts;
  }

  const MyDrawer({
    Key key,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Drawer(
      child: MediaQuery.removePadding(
        context: context,
        child: Padding(
          padding: const EdgeInsets.only(top: 55.0),
          child: ListView(
            children: makeText(),
          ),
        ),
      ),
    );
  }
}

// class MyEndDrawer extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     // TODO: implement build
//     return Drawer(
//       child: Column(
//         children: MyDrawer().makeText(),
//       ),
//     );
//   }
// }

class Route1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('route1'),
      ),
    );
  }
}

class Route2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('Route2'),
      ),
      body: ConstrainedBox(
        constraints: BoxConstraints.expand(),
        child: Stack(
          fit: StackFit.expand,
          alignment: Alignment.center,
          children: <Widget>[
            Container(
              child: Text(
                'hello world',
                style: TextStyle(color: Colors.white),
              ),
              color: Colors.red,
            ),
            Positioned(
              left: 18.0,
              child: Text('i am iron man'),
            ),
            Positioned(
              top: 18.0,
              child: Text('sax'),
            ),
            Positioned(
              bottom: 18.0,
              child: Container(
                width: 140.0,
                height: 80.0,
                child: Image.asset('lib/devpics/a.jpg'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class PaddingTestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('padding'),
          actions: <Widget>[
            UnconstrainedBox(
              child: SizedBox(
                width: 40.0,
                height: 40.0,
                child: CircularProgressIndicator(
                  strokeWidth: 3,
                  valueColor: AlwaysStoppedAnimation(Colors.white70),
                ),
              ),
            ),
            FlatButton(
              child: Text('one'),
              onPressed: () {
                print('aaaa');
              },
            ),
            FlatButton(
              child: Text('two'),
              onPressed: () {
                print('2');
              },
            ),
          ],
        ),
        body: Padding(
          padding: EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(left: 8.0),
                child: Text('hello'),
              ),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Container(
                  // width: 40.0,
                  // height: 100.0,
                  child: SizedBox(
                    width: 200.0,
                    height: 200.0,
                    child: Image.asset('lib/devpics/a.jpg'),
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(20.0, .0, 20.0, 20.0),
                child: Text('letter'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class MatrixRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('matrix'),
        ),
        body: Column(
          children: <Widget>[
            Container(
              margin: const EdgeInsets.all(100.0),
              color: Colors.black,
              width: 200.0,
              height: 55.0,
              child: Transform(
                alignment: Alignment.topRight,
                transform: Matrix4.skewY(0.4),
                child: Container(
                  padding: const EdgeInsets.all(8.0),
                  color: Colors.deepOrange,
                  child: Text(
                    'avengers',
                    style: TextStyle(fontSize: 35.0),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class ScrollRoute extends StatelessWidget {
  List<Widget> makePics() {
    var picsList = List<Image>();
    for (var i = 0; i < 100; i++) {
      picsList.add(Image.asset('lib/devpics/a.jpg'));
    }
    return picsList;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('ListView'),
      mdBody: ListView(
        scrollDirection: Axis.horizontal,
        children: makePics(),
        padding: const EdgeInsets.only(bottom: 300.0),
      ),
    );
  }
}

class MDHead extends StatelessWidget {
  final Widget mdBody;
  final Text mdTitle;
  final Widget mFloatingActionButton;
  MDHead({
    Key key,
    @required this.mdBody,
    @required this.mdTitle,
    this.mFloatingActionButton,
  }) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          title: mdTitle,
        ),
        body: mdBody,
        floatingActionButton: mFloatingActionButton,
      ),
    );
  }
}

class LVBuilderRoute extends StatelessWidget {
  var divider1 = Divider(
    color: Colors.blue,
  );
  var divider2 = Divider(
    color: Colors.red,
  );
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('builder'),
      mdBody: ListView.separated(
        itemCount: 100,
        //force height
        itemBuilder: (BuildContext context, int index) {
          return Image.asset('lib/devpics/a.jpg');
        },
        separatorBuilder: (BuildContext context, int index) {
          return index % 2 == 0 ? divider1 : divider2;
        },
      ),
    );
  }
}

class InfiniteLoadRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InfiniteLoadRouteState();
  }
}

class _InfiniteLoadRouteState extends State<InfiniteLoadRoute> {
  void _retriveData() {
    Future.delayed(Duration(milliseconds: 300)).then((e) {
      print(_words);
      _words.insertAll(_words.length - 1,
          generateWordPairs().take(20).map((e) => e.asPascalCase).toList());

      setState(() {});
    });
  }

  //show message
  static const loadingTag = '##loading##';

  var _words = <String>[loadingTag];
  bool _isLoading;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _retriveData();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('infiniteLoading'),
      mdBody: ListView.separated(
        itemBuilder: (BuildContext context, int index) {
          if (_words[index] == loadingTag) {
            if (_words.length - 1 < 100) {
              _retriveData();
              return Container(
                padding: const EdgeInsets.all(16.0),
                alignment: Alignment.center,
                child: SizedBox(
                  width: 30.0,
                  height: 30.0,
                  child: CircularProgressIndicator(
                    strokeWidth: 2.0,
                  ),
                ),
              );
            } else {
              return Container(
                alignment: Alignment.center,
                padding: const EdgeInsets.all(16.0),
                child: Text(
                  'no more data',
                  style: TextStyle(color: Colors.grey),
                ),
              );
            }
          }
          return ListTile(title: Text(_words[index]));
        },
        itemCount: _words.length,
        separatorBuilder: (c, index) => Divider(
          height: .0,
        ),
      ),
    );
  }
}

class FixedItemRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MDHead(
      mdTitle: Text('FixedItem'),
      mdBody: Column(
        children: <Widget>[
          SizedBox(
            height: 80.0,
            child: Image.asset('lib/devpics/a.jpg'),
          ),
          Expanded(
            //md height of status bar : 24
            //md height of appBar :56
            //md height of texttile:56
            child: ListView.builder(
              itemCount: 200,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  title: Text('$index'),
                );
              },
            ),
          ),
          ListView.custom(
            childrenDelegate: LVCDelegate(),
          )
        ],
      ),
    );
  }
}

class LVCDelegate extends SliverChildDelegate {
  @override
  Widget build(BuildContext context, int index) {
    // TODO: implement build
    return null;
  }

  @override
  bool shouldRebuild(SliverChildDelegate oldDelegate) {
    // TODO: implement shouldRebuild
    return null;
  }
}

class GridViewRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('gridview'),
      mdBody: Column(
        children: <Widget>[
          Expanded(
            child: GridView(
              //GridView.count(count:) => Delegate Count
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3, childAspectRatio: 1.0),
              children: <Widget>[
                Icon(Icons.ac_unit),
                Icon(Icons.ac_unit),
                Icon(Icons.ac_unit),
                Icon(Icons.ac_unit),
                Icon(Icons.ac_unit),
                Icon(Icons.ac_unit),
              ],
            ),
          ),
          Expanded(
            child: GridView(
              gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 120.0,
                childAspectRatio: 1.0,
              ),
              children: <Widget>[
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
                Image.asset('lib/devpics/a.jpg'),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class GetIconGDRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _GetIconGDRoute();
  }
}

class _GetIconGDRoute extends State<GetIconGDRoute> {
  List<IconData> _icons = [];
  void _retriveIcons() {
    Future.delayed(Duration(milliseconds: 500)).then((e) {
      setState(() {
        _icons.addAll([
          Icons.panorama,
          Icons.video_call,
          Icons.hd,
          Icons.fullscreen_exit,
          Icons.landscape,
        ]);
      });
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _retriveIcons();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('get data'),
      mdBody: GridView.builder(
        itemCount: _icons.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          childAspectRatio: 1.0,
        ),
        itemBuilder: (BuildContext context, int index) {
          if (index == _icons.length - 1 && _icons.length < 200) {
            _retriveIcons();
          }

          return Icon(_icons[index]);
        },
      ),
    );
  }
}

class MyCustomGDRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Material(
      child: CustomScrollView(
        slivers: <Widget>[
          SliverPadding(
            padding: const EdgeInsets.only(top: 24.0),
            sliver: SliverAppBar(
              //will cover the flexiblespace title
              // title: Text('sliver appbar'),
              expandedHeight: 300.0,
              pinned: true,
              flexibleSpace: FlexibleSpaceBar(
                title: Text('flexSapceBar'),
                background: Image.asset('lib/devpics/a.jpg'),
              ),
            ),
          ),
          SliverPadding(
            padding: const EdgeInsets.all(8.0),
            sliver: SliverGrid(
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                mainAxisSpacing: 10.0,
                crossAxisSpacing: 10.0,
                childAspectRatio: 4.0,
              ),
              delegate: SliverChildBuilderDelegate(
                (context, index) {
                  return Container(
                    alignment: Alignment.center,
                    color: Colors.cyan[100 * (index % 9)],
                    child: Text('grid item $index'),
                  );
                },
                childCount: 20,
              ),
            ),
          ),
          SliverFixedExtentList(
            itemExtent: 50.0,
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return Container(
                  alignment: Alignment.center,
                  color: Colors.lightBlue[100 * (index % 9)],
                  child: Text('list item $index'),
                );
              },
              childCount: 50,
            ),
          ),
        ],
      ),
    );
  }
}

class MyScrollControllerRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyScrollControllerRouteState();
  }
}

class _MyScrollControllerRouteState extends State<MyScrollControllerRoute> {
  var _controller = ScrollController();

  bool showToTopBtn = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller.addListener(
      () {
        print(_controller.offset);
        if (_controller.offset < 1000 && showToTopBtn) {
          setState(
            () {
              showToTopBtn = false;
            },
          );
        } else if (_controller.offset >= 1000 && showToTopBtn == false) {
          setState(() {
            showToTopBtn = true;
          });
        }
      },
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('Controller'),
      mdBody: Scrollbar(
        child: ListView.builder(
          itemCount: 109,
          itemExtent: 80.0,
          controller: _controller,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text('$index'),
            );
          },
        ),
      ),
      mFloatingActionButton: !showToTopBtn
          ? null
          : FloatingActionButton(
              child: Icon(Icons.arrow_upward),
              onPressed: () {
                _controller.animateTo(
                  .0,
                  duration: Duration(milliseconds: 200),
                  curve: Curves.ease,
                );
              },
            ),
    );
  }
}

class MyVimRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MDHead(
      mdTitle: Text('FromVim'),
      mdBody: Scaffold(
        body: Center(
          child: RaisedButton(
            onPressed: () {},
            child: Text('touch henshi'),
          ),
        ),
      ),
    );
  }
}

class ScrollNotificationRoute extends StatefulWidget {
  @override
  _ScrollNotificationRouteState createState() =>
      _ScrollNotificationRouteState();
}

class _ScrollNotificationRouteState extends State<ScrollNotificationRoute> {
  //percentage of progress
  String _progress = '0%';
  @override
  Widget build(BuildContext context) {
    return MDHead(
      mdTitle: Text('percent'),
      mdBody: Scrollbar(
        child: NotificationListener<ScrollNotification>(
          onNotification: (ScrollNotification notification) {
            double progress = notification.metrics.pixels /
                notification.metrics.maxScrollExtent;
            setState(() {
              _progress = '${(progress * 100).toInt()}%';
            });
            print('bottomEdge:${notification.metrics.extentAfter == 0}');
            // return true;
          },
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              ListView.builder(
                  itemCount: 100,
                  itemExtent: 50.0,
                  itemBuilder: (context, index) {
                    return ListTile(title: Text('$index'));
                  }),
              //show percentage of progress
              CircleAvatar(
                radius: 30.0,
                child: Text(_progress),
                backgroundColor: Colors.black54,
              )
            ],
          ),
        ),
      ),
    );
  }
}

class WillPopRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _WillPopRouteState();
  }
}

class _WillPopRouteState extends State<WillPopRoute> {
  //last tapped
  DateTime _lastTapped;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('拦截'),
      mdBody: WillPopScope(
        onWillPop: () async {
          print('does juge');
          //taps between 1 second
          if (_lastTapped == null ||
              DateTime.now().difference(_lastTapped) > Duration(seconds: 1)) {
            _lastTapped = DateTime.now();
            print('not exit');
            return false;
          }
          print('exit');
          return true;
        },
        child: Container(
          alignment: Alignment.center,
          child: Text('taps in 1 second to exit'),
        ),
      ),
    );
  }
}

class ShareDataWidget extends InheritedWidget {
  ShareDataWidget({
    @required this.data,
    Widget child,
  }) : super(child: child);
  final int data;

  static ShareDataWidget of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(ShareDataWidget);
  }

  @override
  bool updateShouldNotify(ShareDataWidget oldWidget) {
    // TODO: implement updateShouldNotify
    //oldWidget before setState (last setState)
    // in this widget after setState
    return oldWidget.data != data;
  }
}

class _TestWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _TestWidgetState();
  }
}

class _TestWidgetState extends State<_TestWidget> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text(ShareDataWidget.of(context).data.toString());
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print('dependencies changed');
  }
}

class InheritedTestRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _InheritedTestRouteState();
  }
}

class _InheritedTestRouteState extends State<InheritedTestRoute> {
  int count = 0;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('share data'),
      mdBody: Center(
        child: ShareDataWidget(
          data: count,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.only(bottom: 20.0),
                child: _TestWidget(),
              ),
              RaisedButton(
                child: Text('increment'),
                onPressed: () => setState(() => ++count),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class ChangeColorRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ChangeColorRouteState();
  }
}

class _ChangeColorRouteState extends State<ChangeColorRoute> {
  Color _themeColor = Colors.teal;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ThemeData themeData = Theme.of(context);
    return Theme(
      data: ThemeData(
        //theme color
        primarySwatch: _themeColor,
        iconTheme: IconThemeData(color: _themeColor),
      ),
      child: Scaffold(
        appBar: AppBar(
          title: Text('color theme'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Icon(Icons.favorite),
                Icon(Icons.airport_shuttle),
                Text('color with theme'),
              ],
            ),
            Theme(
              data: themeData.copyWith(
                iconTheme: themeData.iconTheme.copyWith(color: Colors.black),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Icon(Icons.favorite),
                  Icon(Icons.airport_shuttle),
                  Text('fixed color'),
                ],
              ),
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => setState(
            () => _themeColor =
                _themeColor == Colors.teal ? Colors.lightBlue : Colors.teal,
          ),
          child: Icon(Icons.palette),
        ),
      ),
    );
  }
}

class MyListenerRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createStatere
    return _MyListenerRouteState();
  }
}

class _MyListenerRouteState extends State<MyListenerRoute> {
  PointerEvent _event;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Listener(
      child: Container(
        alignment: Alignment.center,
        color: Colors.lightBlue,
        width: 300.0,
        height: 150.0,
        child: Text(
          _event?.toString() ?? '',
          style: TextStyle(
            color: Colors.white,
            fontSize: 25.0,
          ),
        ),
      ),
      onPointerDown: (PointerDownEvent event) => setState(() => _event = event),
      onPointerMove: (PointerMoveEvent event) => setState(() => _event = event),
      onPointerUp: (PointerUpEvent event) => setState(() => _event = event),
    );
  }
}

class MyDragRoute extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyDragRouteState();
  }
}

class _MyDragRouteState extends State<MyDragRoute>
    with SingleTickerProviderStateMixin {
  //from top
  double _top = 0.0;
  //from left
  double _left = 0.0;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('drag'),
      mdBody: Stack(
        children: <Widget>[
          Positioned(
            top: _top,
            left: _left,
            child: GestureDetector(
              child: CircleAvatar(
                child: Text('A'),
              ),
              onPanDown: (DragDownDetails d) {
                print('press down ${d.globalPosition}');
              },
              onPanUpdate: (d) {
                setState(() {
                  _left += d.delta.dx;
                  _top += d.delta.dy;
                });
              },
              onPanEnd: (d) {
                print(d.velocity);
              },
            ),
          ),
        ],
      ),
    );
  }
}

class MyZoomGesture extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyZoomGestureState();
  }
}

class _MyZoomGestureState extends State<MyZoomGesture> {
  double _width = 200.0;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MDHead(
      mdTitle: Text('ZoomPicture'),
      mdBody: Center(
        child: GestureDetector(
          child: Image.asset(
            'lib/devpics/a.jpg',
            width: _width,
          ),
          onScaleUpdate: (d) {
            setState(
              () {
                _width = 200 * d.scale.clamp(.7, 10.0);
              },
            );
          },
        ),
      ),
    );
  }
}
